﻿package
{
	import com.greensock.TweenLite;
	
	import core.manager.SoundManager;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	import core.Config;
	
	/**
	 * 牌的效果控制
	 */ 
	public class CardEffect extends MovieClip
	{
		private var startFrame:int = -1;
		private var endFrame:int = -1;
		
		private var func:Function = null;//函数用来控制循环播放
		
		public var card:CardObject;//cardEffect中应用到cardObject元件，进行定义
		
		public function CardEffect()
		{
			super();
			this.stop();
		}
		
		/**
		 * 先移除enteFrame事件，再进行触发enterFrame 事件防止起冲突产生bug
		 * */
		private function checkState():void {
			this.removeEventListener(Event.ENTER_FRAME,onnterFrameHandler);
			this.addEventListener(Event.ENTER_FRAME,onnterFrameHandler);
		}
		
		/**
		 * 利用enteFrame  事件来循环播放每一帧，因为素材有bug
		 * */
		private var clearID:int;
		
		private function onnterFrameHandler(event:Event):void
		{
			startFrame++;
			if(startFrame > endFrame)//如果开始的帧数大于结尾的帧数
			{
				this.removeEventListener(Event.ENTER_FRAME,onnterFrameHandler);
				if(this.func != null){
					this.func();
					this.func = null;
				}
				if(this.callback != null){
					clearID = setTimeout(this.callback,1000);
				}
			} else  {
				this.gotoAndStop(startFrame);
			}
			this.update();
		}
		
		private function callbackLater():void {
			clearTimeout(clearID);
			this.callback();
			this.callback = null;
		}
		/**
		 * 发牌动作
		 */
		public var callback:Function;
		
		public function showCardEffect():void
		{
			this.startFrame = 0;
			this.endFrame = 8;
			this.func = stopCardEffect;// 动画播放完停止到指定的位置
			this.checkState();
		}
		
		public function hideCardEffect():void {
			/*this.startFrame = 16;
			this.endFrame = 20;
			this.func = this.flip;
			this.checkState();*/
			TweenLite.to(this.card,0.1,{scaleX:-1,onUpdate:onUpdate,onComplete:onComplete});
		}
		
		private function onUpdate():void {
			if(this.card.scaleX <= -0.5){
				this.flip();
			}
		}
		
		/**
		 * 到达中点,需要一个动画
		 */ 
		private function onComplete():void {
			
		}
		
		public function stopCardEffect():void {
			this.gotoAndStop(15);
			this.update();
		}
		
		private var _data:Object;
		
		public function set data(value:Object):void {
			this._data = value;
			this.update();
		}
		
		public function get data():Object {
			return this._data;
		}
		
		public function update():void {
			if(this.card){
				this.card.data = this._data;
			}
		}
		
		public function flip(soundabled:Boolean = true):void{
			if(this.currentFrame != Config.CARD_OBJECT_REVERSE_FRAME){
				this.card.gotoAndStop(Config.CARD_OBJECT_REVERSE_FRAME);
			}
			if(soundabled){
				SoundManager.instance.play("card_ends");
			}
			//var position:Point = this.parent.parent.localToGlobal(Config.REMOVE_CARD_POSITION)
			//TweenLite.to(this,0.3,{x:position.x,y:position.y,onComplete:toDestination});// TODO 
		}
		
		
		private function toDestination():void {
			if(this.parent){
				this.parent.removeChild(this);
			}
		}
		
		public var owner:int = -1;// 属于哪个人的牌
	}
}